package com.hln.jingwushi.ui

import android.os.Bundle
import android.util.Log
import com.hln.jingwushi.MyBaseActivity
import com.hln.jingwushi.R
import com.hln.jingwushi.databinding.ActivityLightBinding
import com.hln.jingwushi.utils.DataCacheUtil
import com.gdsz.libbasic.utils.assist.getCurrentDataLong
import com.gdsz.libbasic.utils.assist.getWeek
import com.gdsz.libbasic.utils.assist.getYmd
import com.gdsz.libbasic.utils.assist.weekArray
import com.gdsz.libbasic.utils.view.throttleClick
import com.zgkxzx.modbus4And.requset.ModbusReq
import com.zgkxzx.modbus4And.requset.OnRequestBack

class LightActivity : MyBaseActivity<ActivityLightBinding>() {
    var isOpen = 0
    override fun ActivityLightBinding.initBinding(savedInstanceState: Bundle?) {
        readInputRegistersClickEvent()
        val currentDate = getCurrentDataLong()
        binding.tvDate.text = getYmd(currentDate) + "   ${weekArray[getWeek(currentDate)]}"
        binding.tvName.text = DataCacheUtil.getName()
        binding.tvTip.text =
            "方舱运行状态正常，紧急联系电话：${DataCacheUtil.getContactTel()}，紧急联系人：${DataCacheUtil.getContactPerson()}"
    }

    override fun onClick() {
        super.onClick()
        binding.switchControl.throttleClick {
            if (isOpen == 1) {
                closeClickEvent()
            } else {
                openClickEvent()
            }
        }
    }


    fun openClickEvent() {
        ModbusReq.getInstance().writeRegisters(object : OnRequestBack<String?> {
            override fun onSuccess(t: String?) {
                isOpen = 1
                updateOpenState()
            }

            override fun onFailed(msg: String) {
                isOpen = 0
                updateOpenState()
            }
        }, 1, 0, shortArrayOf(1))
    }

    fun closeClickEvent() {
        ModbusReq.getInstance().writeRegisters(object : OnRequestBack<String?> {
            override fun onSuccess(t: String?) {
                isOpen = 0
                updateOpenState()
            }

            override fun onFailed(msg: String) {

            }
        }, 1, 0, shortArrayOf(0))
    }

    fun readInputRegistersClickEvent() {
        ModbusReq.getInstance().readInputRegisters(object : OnRequestBack<ShortArray> {
            override fun onSuccess(data: ShortArray) {
                Log.d(
                    "asd",
                    "readInputRegisters onSuccess ${data[0].toInt()} ，${Thread.currentThread().name}"
                )
                runOnUiThread {
                    isOpen = data[0].toInt()
                    updateOpenState()
                }
            }

            override fun onFailed(msg: String) {
                Log.e("asd", "readInputRegisters onFailed $msg")
            }
        }, 1, 0, 10)
    }

    override fun notifyData(values: ShortArray) {
        runOnUiThread {
            binding.tvTemp.text = "温度 ${values[0]} ℃  湿度 ${values[1]} %"
        }
    }

    private fun updateOpenState() {
        if (isOpen == 1) {
            binding.switchControl.setImageResource(R.mipmap.selector)
        } else {
            binding.switchControl.setImageResource(R.mipmap.unselector)
        }
    }
}